package com.xframe.xdal.core.manage;

import com.xframe.xdal.core.annotation.XColumn;
import com.xframe.xdal.core.model.mapping.IColumnMapping;
import com.xframe.xdal.core.model.mapping.IRelation;
import com.xframe.xdal.core.model.mapping.ITableInfo;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
public class DBTableInfo implements ITableInfo {

    public static final String KEY = "key";
    public static final String TABLENAME = "tableName";
    /**
     * ID
     */
    @XColumn
    private String tableID;
    /**
     * 数据表名
     */
    @XColumn
    private String tableName;

    /**
     * 类型 视图 = view  表= table
     */
    @XColumn
    private String tableType;

    /**
     * 数据表的注释
     */
    @XColumn
    private String tableDesc;

    /**
     * 列信息
     */
    private List<DBColumnInfo> dbColumnInfoList;

    @Override
    public String getName() {
        return tableName;
    }

    @Override
    public String getCode() {
        return tableName;
    }

    @Override
    public String getAsName() {
        return tableName;
    }

    @Override
    public String getLabel() {
        return tableDesc;
    }

    @Override
    public String getIndexName() {
        return tableName;
    }

    @Override
    public String getDataSourceDefCode() {
        return "main";
    }

    @Override
    public List<IColumnMapping> getColumnMappingList() {
        return new ArrayList<>(dbColumnInfoList);
    }

    @Override
    public void setColumnMappingList(List<IColumnMapping> columnMappingList) {

    }

    @Override
    public List<IRelation> getBelongsToMappingList() {
        return null;
    }

    @Override
    public List<IRelation> getHasManyMappingList() {
        return null;
    }

    @Override
    public IColumnMapping findOneByFieldName(String fieldName) {
        IColumnMapping columnMapping = null;
        for (int index = 0; this.dbColumnInfoList != null && index < dbColumnInfoList.size(); index++) {
            if (this.dbColumnInfoList.get(index).getFieldName().equals(fieldName)) {
                columnMapping = this.dbColumnInfoList.get(index);
                break;
            }
        }
        return columnMapping;
    }

    @Override
    public IColumnMapping findOneByName(String name) {
        IColumnMapping columnMapping = null;
        for (int index = 0; this.dbColumnInfoList != null && index < dbColumnInfoList.size(); index++) {
            if (this.dbColumnInfoList.get(index).getFieldName().equals(name)) {
                columnMapping = this.dbColumnInfoList.get(index);
                break;
            }
        }
        return columnMapping;
    }

    @Override
    public IColumnMapping findPkFirst() {
        IColumnMapping columnMapping = null;
        for (int index = 0; this.dbColumnInfoList != null && index < this.dbColumnInfoList.size(); index++) {
            if (this.dbColumnInfoList.get(index).getPrimaryKeyFlag()) {
                columnMapping = this.dbColumnInfoList.get(index);
                break;
            }
        }
        return columnMapping;
    }

    @Override
    public IColumnMapping findVer() {
        IColumnMapping columnMapping = null;
        for (int index = 0; this.dbColumnInfoList != null && index < this.dbColumnInfoList.size(); index++) {
            if (this.dbColumnInfoList.get(index).getVersionFlag()) {
                columnMapping = this.dbColumnInfoList.get(index);
                break;
            }
        }
        return columnMapping;
    }

    @Override
    public List<IColumnMapping> findUniqueKeyList() {
        List<IColumnMapping> findList = new ArrayList<>();
        for (int index = 0; this.dbColumnInfoList != null && index < this.dbColumnInfoList.size(); index++) {
            // 取不同的判断条件
            if (this.dbColumnInfoList.get(index).getUniqueFlag()) {
                findList.add(this.dbColumnInfoList.get(index));
            }

        }
        return findList;
    }

    @Override
    public List<IColumnMapping> findCanUpdateList() {
        List<IColumnMapping> findList = new ArrayList<>();
        for (int index = 0; dbColumnInfoList != null && index < dbColumnInfoList.size(); index++) {
            IColumnMapping columnMapping = dbColumnInfoList.get(index);
            if (!columnMapping.getPrimaryKeyFlag() && !columnMapping.getUniqueFlag() && !columnMapping.getReadonly() && !columnMapping.getVersionFlag()) {
                findList.add(dbColumnInfoList.get(index));
            }
        }
        return findList;
    }
}
